/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 */
package org.sbbs;

import jakarta.annotation.Resource;
import java.util.concurrent.TimeUnit;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.autoconfigure.RocketMQAutoConfiguration;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Service;

/**
 *
 * @author Mr.FDC <fdc_messenger@126.com>
 */
@SpringBootApplication
@Import(RocketMQAutoConfiguration.class)
public class SBRocket implements CommandLineRunner {

    @Resource
    private RocketMQTemplate rocketMQTemplate;

    @Value("${rocketmq.producer.simple.demo.topic}")
    private String topic;

    public static void main(String[] args) {
        SpringApplication.run(SBRocket.class, args);
    }

    @Override
    public void run(String... args) throws InterruptedException {
        for (int i = 0; i < 1000; i++) {
            SendResult sendResult = rocketMQTemplate.syncSend(topic, "Hello, World1!");
            TimeUnit.MILLISECONDS.sleep(10);
            System.out.println(sendResult);
        }
//        for (int i = 0; i < 1000; i++) {
//            rocketMQTemplate.asyncSend(topic, "Hello, World!", new SendCallback() {
//                @Override
//                public void onSuccess(SendResult sendResult) {
//                    System.out.printf("异步发送成功: %s", sendResult);
//                }
//                @Override
//                public void onException(Throwable throwable) {
//                    System.out.printf("异步发送失败: %s", throwable.getMessage());
//                }
//            });
//        }
//        System.out.println(sendResult);
    }

    @Service
    @RocketMQMessageListener(topic = "demo-topic", consumerGroup = "demo-group")
    public static class DemoConsumer implements RocketMQListener<String> {

        public void onMessage(String message) {
            System.out.println("received message: " + message);
        }
    }
}
